package com.haoniu.tenant.service.impl;

import java.util.List;
import com.haoniu.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.haoniu.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.haoniu.tenant.domain.SysUse;
import com.haoniu.tenant.mapper.TbBusinessMapper;
import com.haoniu.tenant.domain.TbBusiness;
import com.haoniu.tenant.service.ITbBusinessService;

/**
 * 商户Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-19
 */
@Service
public class TbBusinessServiceImpl implements ITbBusinessService 
{
    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    /**
     * 查询商户
     * 
     * @param id 商户主键
     * @return 商户
     */
    @Override
    public TbBusiness selectTbBusinessById(Long id)
    {
        return tbBusinessMapper.selectTbBusinessById(id);
    }

    /**
     * 查询商户列表
     * 
     * @param tbBusiness 商户
     * @return 商户
     */
    @Override
    public List<TbBusiness> selectTbBusinessList(TbBusiness tbBusiness)
    {
        return tbBusinessMapper.selectTbBusinessList(tbBusiness);
    }

    /**
     * 新增商户
     * 
     * @param tbBusiness 商户
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbBusiness(TbBusiness tbBusiness)
    {
        tbBusiness.setCreateTime(DateUtils.getNowDate());
        int rows = tbBusinessMapper.insertTbBusiness(tbBusiness);
        insertSysUse(tbBusiness);
        return rows;
    }

    /**
     * 修改商户
     * 
     * @param tbBusiness 商户
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbBusiness(TbBusiness tbBusiness)
    {
        tbBusinessMapper.deleteSysUseByUserId(tbBusiness.getId());
        insertSysUse(tbBusiness);
        return tbBusinessMapper.updateTbBusiness(tbBusiness);
    }

    /**
     * 批量删除商户
     * 
     * @param ids 需要删除的商户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbBusinessByIds(Long[] ids)
    {
        tbBusinessMapper.deleteSysUseByUserIds(ids);
        return tbBusinessMapper.deleteTbBusinessByIds(ids);
    }

    /**
     * 删除商户信息
     * 
     * @param id 商户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbBusinessById(Long id)
    {
        tbBusinessMapper.deleteSysUseByUserId(id);
        return tbBusinessMapper.deleteTbBusinessById(id);
    }

    /**
     * 新增系统用户信息
     * 
     * @param tbBusiness 商户对象
     */
    public void insertSysUse(TbBusiness tbBusiness)
    {
        List<SysUse> sysUseList = tbBusiness.getSysUseList();
        Long id = tbBusiness.getId();
        if (StringUtils.isNotNull(sysUseList))
        {
            List<SysUse> list = new ArrayList<SysUse>();
            for (SysUse sysUse : sysUseList)
            {
                sysUse.setUserId(id);
                list.add(sysUse);
            }
            if (list.size() > 0)
            {
                tbBusinessMapper.batchSysUse(list);
            }
        }
    }
}
